package cn.zbmao.web.controller.user;

import cn.zbmao.web.bean.User;
import cn.zbmao.web.util.RegexUtil;
import cn.zbmao.web.util.dao.DaoFactory;
import cn.zbmao.web.util.mail.SignupMail;
import com.jfinal.core.Controller;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

//找回密码
public class ResetPwdController extends Controller {
	public void index(){
		setAttr("title","找回密码");
        setAttr("param","register");
        render("/views/account/find_password/find_password.html");
	}

    public void process_success(){
        setAttr("title","找回密码");
        setAttr("param","register");
        setAttr("process_success_email",getPara("email")==null?"":getPara("email"));
        render("/views/account/find_password/process_success.html");
    }

    /**
     * 通过邮件找回密码
     */
    public void request_find_password(){
        Map<String,String> rsm = null;
        int errno;
        String err;

        String email = getPara("email",null);
        String sessionCaptcha = getSessionAttr("sessionCaptcha");
        String captcha = getPara("seccode_verify",null);

        if (StringUtils.isEmpty(captcha)||!captcha.equals(sessionCaptcha)){
            errno = -1;
            err="验证码错误!";
            setAttr("err",err);
            setAttr("errno",errno);
            setAttr("rsm", rsm);
            renderJson();
            return;
        }


        if (StringUtils.isEmpty(email)){
            errno = -1;
            err="email 地址为空，请重新输入email地址";
            setAttr("err",err);
            setAttr("errno",errno);
            setAttr("rsm", rsm);
            renderJson();
            return;
        }else if (!RegexUtil.isEmail(email)) {
            errno = -1;
            err="email 格式不正确!";
            setAttr("err",err);
            setAttr("errno",errno);
            setAttr("rsm",rsm);
            renderJson();
            return;
        }else if(!DaoFactory.getUserDao().isUserEmail(email)){
            errno = -1;
            err="email 错误,不存在此email";
            setAttr("err",err);
            setAttr("errno",errno);
            setAttr("rsm",rsm);
            renderJson();
            return;
        }else{
            errno = 1;
            err="";

//          getSession().setAttribute("process_success_email", email);

            new SignupMail().sendProcessSuccessMail(email);
            rsm = new HashMap<String, String>();
            rsm.put("url","/resetpwd/process_success?email="+email);
            setAttr("err", err);
            setAttr("errno",errno);
            setAttr("rsm",rsm);
            renderJson();
            return;
        }
    }

    public void modify(){
        String md5 = getPara("key","0");
        User user = DaoFactory.getUserDao().findMd5(md5);
        if (user!=null) {
            getSession().setAttribute("user_id", user.get("id"));
            setAttr("title", "找回密码");
            setAttr("param", "register");

            render("/views/account/find_password/modify.html");
        }else {
            renderError(404);
        }
    }



    public void find_password_modify(){
        Map<String,String> rsm = null;
        int errno;
        String err;

        String sessionCaptcha = getSessionAttr("sessionCaptcha");
        String captcha = getPara("seccode_verify",null);

        if (StringUtils.isEmpty(captcha)||!captcha.equals(sessionCaptcha)){
            errno = -1;
            err="验证码错误!";
            setAttr("err",err);
            setAttr("errno",errno);
            setAttr("rsm", rsm);
            renderJson();
            return;
        }
        String password = getPara("password");
        String re_password = getPara("re_password");
        if (!StringUtils.isEmpty(password)&&!StringUtils.isEmpty(re_password)){
            if(password.equals(re_password)){
               String bl = DaoFactory.getUserDao().updatePassword((Integer)getSession().getAttribute("user_id"),password);
               if (!StringUtils.isEmpty(bl)){
                   err="修改成功";
                   errno=1;

                   User user = DaoFactory.getUserDao().getUserByName(bl);
                   getSession().setAttribute("user", user);


                   rsm=new HashMap<String, String>();
                   rsm.put("url","/news");
                   setAttr("err", err);
                   setAttr("errno",errno);
                   setAttr("rsm",rsm);
                   renderJson();
                   return;
               }
            }
        }
        err = "密码输入有误!";
        errno = -1 ;
        setAttr("err", err);
        setAttr("errno",errno);
        setAttr("rsm",rsm);
        renderJson();
        return;
    }

}
